﻿using CommunityToolkit.Mvvm;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using config.model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using config.view;
using config.service;
using Accessibility;
namespace config.viewmodel
{
    partial class ViewModel: ObservableObject 
    {
        private ISerial _serial;

        public RelayCommand GoHome { get; } 
        public ObservableCollection<NavigateItem> MenuItems { get; } = new ObservableCollection<NavigateItem>();
        public RelayCommand FlashDown { get; }
        public ViewModel(ISerial serial)
        {
            this._serial = serial;
            MenuItems.Add(new NavigateItem("首页", "Home"));
            MenuItems.Add(new NavigateItem("基本设置", "BasicSet"));
            MenuItems.Add(new NavigateItem("专业设置", "AdvancedSet"));
            MenuItems.Add(new NavigateItem("频率均衡器", "FrequencySet"));
            MenuItems.Add(new NavigateItem("力反馈曲线", "InputMappingSet"));
            MenuItems.Add(new NavigateItem("按键映射", "InputMappingSet"));
            SelectedItem = MenuItems.First();
            GoHome = new RelayCommand(() =>
            {
                SelectedItem = MenuItems.First();
            });
            FlashDown = new RelayCommand(OnFlashDown);
        }
        [ObservableProperty]
        private NavigateItem? _selectedItem;

        [ObservableProperty]
        private UserControl _currentPage;
        partial void OnSelectedItemChanged(NavigateItem? value)
        {
            if (value == null) return;
            CurrentPage = Helper.ViewHelper.LoadPage(value?.PageName);
        }
        private void OnFlashDown()
        {
            bool result = MyMessageBox.ShowQuestion("确定将数据保存到设备Flash中吗？", "操作确认");
            if (result)
            {
                if (_serial.IsConnected)
                {
                    var dataToSend = FFBProtocolParser.CreateCommandFrame(FFBProtocolParser.FFBCommand.CMD_SAVE_PARAMS);
                    _serial.SendData(dataToSend);

                }
            }
        }
    }
}
